package ir.values.users.instructions.mem;

import frontend.visitor.symboltbl.scope.Scope;
import ir.types.datatypes.PointerType;
import ir.values.users.instructions.InstOperator;
import ir.values.users.instructions.Instruction;

public class AllocaInst extends Instruction {
    private final Scope parentScope;
    public AllocaInst(String originalName, PointerType type, Scope parentScope) {
        super(originalName, type);
        this.parentScope = parentScope;
    }
    @Override
    public boolean shouldGenerateId() {
        return false;
    }
    public PointerType getPointerType() {
        return (PointerType) type;
    }

    @Override
    public InstOperator getInstOperator() {
        return InstOperator.alloca;
    }
    @Override
    public String parseUsingName() {
        return "%" + originalName + "_" + parentScope.getName();
    }
    @Override
    public String parseLLVM() {
        StringBuilder sb = new StringBuilder();
        sb
                .append(parseUsingName())
                .append(" = ")
                .append(getInstOperator())
                .append(" ")
                .append(getPointerType().getBaseType().toString());
        return sb.toString();
    }
}
